#-------------------------------------------------------------------------------
# Name:        formatter.py
# Purpose:     Generate slidespeech output for distribution in various formats
#
# Authors:      Glenn Ramsey <glenn.ramsey@slidespeech.com>
#
# Copyright:   (c) Slidespeech Ltd 2012
# Licence:     MIT license
#-------------------------------------------------------------------------------

# This is now depracated and will be removed soon


import os, shutil
import zipfile
import json
from tempfile import mktemp
from audio_converter import textToSpeech

class Formatter:
    def create(self, script):
        """Create the output format from the given script dict"""
        pass
    
    
class ZipFormatter(Formatter):
    def __init__(self, previous, zipPath):
        self._previous = previous
        self._zipPath = zipPath
        
    def create(self, script):
        

        # Append to the zipfile, so that different formatters can add
        # content to the same file.
        z = zipfile.ZipFile(self._zipPath, 'a')
        
        try:
            seq = script['S']
            
            # Write the image file to the zip and modify the path to reflect its
            # location in the zip file.
            for slide in seq:
                imagePath = slide['i']
                (head, tail) = os.path.split(imagePath)
                z.write(imagePath, "images/"+tail, zipfile.ZIP_DEFLATED)
                # update the script image path to reflect its location in the zip archive
                slide['i'] = "images/"+tail
            
        except KeyError:
            pass

        
        jsonString = json.dumps(script)
        z.writestr("script.json", jsonString, zipfile.ZIP_DEFLATED)
        
        z.close()
       
       
class AudioFormatter(Formatter):
    def __init__(self, zipPath):
        self._zipPath = zipPath

    def create(self, script):
        
        
        audioDir = mktemp()
        try:
            os.makedirs(audioDir)
        except OSError:
            # dir already exists
            pass

        # Append to the zipfile, so that different formatters can add
        # content to the same file.
        z = zipfile.ZipFile(self._zipPath, 'a')
        
        audioTypes = [".ogg", ".mp3"]
        
        try:
            seq = script['S']
            
            # Create a list of speaker notes so they can be sent to the TTS engine all at once
            speakerNoteList = []
            for i, slide in enumerate(seq):
                speakerNote = slide['q']
                speakerNoteList.append(speakerNote)

            textToSpeech(speakerNoteList, "en", audioDir, "Slide")

            # copy the audio files into the zip
            for i in len(seq):
                audioNames = []
                for audioType in audioTypes:
                    audioNames.append(os.path.join(audioDir, "Slide{0}{1}".format(i, audioType)))
                
                for audioPath in audioNames:
                    (head, tail) = os.path.split(audioPath)
                    z.write(audioPath, "audio/"+tail, zipfile.ZIP_DEFLATED)
            
        except KeyError:
            pass
        
        z.close()
        
        shutil.rmtree(audioDir)
        
    